package com.pucong.gitee.database.backup.business.unRelational.redis.controller;

import com.pucong.gitee.database.backup.business.unRelational.redis.service.DataSinglesService;
import com.pucong.gitee.database.backup.business.unRelational.redis.util.RedisNode;
import com.pucong.gitee.database.backup.common.entity.redis.KeyBean;
import com.pucong.gitee.database.backup.common.entity.redis.ZTreeBean;
import com.pucong.gitee.database.backup.common.entity.redis.dto.*;
import com.pucong.gitee.database.backup.common.selfenum.RedisDataType;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 单机模式下数据处理器
 */
@RestController
@RequestMapping("/auto/redis/dataSingles")
@Api(value = "redis dataSingles-API")
public class DataSinglesController {

    @Autowired
    private DataSinglesService dataSinglesService;

    @GetMapping("/treeInit/{id}")
    @ApiOperation(value = "第一层 打开连接，返回数据库")
    public Res<List<ZTreeBean>> treeInit(@ApiParam("主键") @PathVariable("id") String id) {
        List<ZTreeBean> zTreeBeans = dataSinglesService.treeInit(id);
        return Res.ok(zTreeBeans);
    }

    @GetMapping("/likeInit/{id}")
    @ApiOperation(value = "第一层 模糊匹配初始化DB树")
    public Res<ZTreeBean> likeInit(@ApiParam("主键") @PathVariable("id") String id, int index, String pattern) {
        ZTreeBean zTreeBean = dataSinglesService.likeInit(id, index, pattern);
        return Res.ok(zTreeBean);
    }

    @GetMapping("/treeData/{id}")
    @ApiOperation(value = "第二层 数据分页查询key")
    public Res<List<ZTreeBean>> treeData(@ApiParam("主键") @PathVariable("id") String id, int index, String pattern) {
        List<ZTreeBean> zTreeBeans = dataSinglesService.treeData(id, index, pattern);
        return Res.ok(zTreeBeans);
    }

    @GetMapping("/pageData/{id}")
    @ApiOperation(value = "第二层 数据分页查询（scan）")
    public Res<List<ZTreeBean>> pageData(@ApiParam("主键") @PathVariable("id") String id, int index, int page, String pattern) {
        List<ZTreeBean> zTreeBeans = dataSinglesService.pageData(id, index, page, pattern);
        return Res.ok(zTreeBeans);
    }

    @GetMapping("/keysData/{id}")
    @ApiOperation(value = "查询KEY详细信息")
    public Res<KeyBean> keysData(@ApiParam("主键") @PathVariable("id") String id, int index, String keys, @ApiParam("desc asc 排序") String order) {
        KeyBean keyBean = dataSinglesService.keysData(id, index, keys, order);
        return Res.ok(keyBean);
    }

    @PostMapping("/renameKeyAndRetimeKey/{id}")
    @ApiOperation(value = "修改key名称和时长")
    public Res<Boolean> renameKey(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RenameKeyRedisDto dto) {
        Boolean aBoolean = dataSinglesService.renameKey(id, dto.getIndex(), dto.getOldKey(), dto.getNewKey());
        if(dto.getOldTime() != dto.getNewTime()){
            Boolean aBoolean2 = dataSinglesService.retimeKey(id, dto.getIndex(), dto.getNewKey(), dto.getNewTime());
        }
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteKey/{id}")
    @ApiOperation(value = "删除key")
    public Res<Boolean> deleteKey(@ApiParam("主键") @PathVariable("id") String id, RedisKeyRemoveDto dto) {
        Boolean aBoolean = dataSinglesService.deleteKey(id, dto.getIndex(), dto.getKey());
        return Res.ok(aBoolean);
    }

    @PostMapping("/updateStr/{id}")
    @ApiOperation(value = "修改字符串值")
    public Res<Boolean> updateStr(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RedisKeyUpdateStrDto dto) {
        Boolean aBoolean = dataSinglesService.updateStr(id, dto.getIndex(), dto.getKey(), dto.getVal());
        return Res.ok(aBoolean);
    }

    @PostMapping("/insertVal/{id}")
    @ApiOperation(value = "新增值")
    public Res<Boolean> insertVal(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RedisValAddStrDto dto) {
        Boolean aBoolean = dataSinglesService.insertVal(id, dto.getIndex(), dto.getType(), dto.getKey(), dto.getVal(), dto.getScore());
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteVal/{id}")
    @ApiOperation(value = "删除值")
    public Res<Boolean> deleteVal(@ApiParam("主键") @PathVariable("id") String id, int index, RedisDataType type, String key, String val) {
        Boolean aBoolean = dataSinglesService.deleteVal(id, index, type, key, val);
        return Res.ok(aBoolean);
    }

    @PostMapping("/updateVal/{id}")
    @ApiOperation(value = "修改val的值")
    public Res<List<RedisNode>> updateVal(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RedisValUpdateDto dto) {
        Boolean aBoolean = dataSinglesService.updateVal(id, dto);
        return Res.ok(aBoolean);
    }

    @PostMapping("/insertKey/{id}")
    @ApiOperation(value = "新增key")
    public Res<Boolean> insertKey(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RedisKeyAddDto dto) {
        Boolean aBoolean = dataSinglesService.insertKey(id, dto.getIndex(), dto.getType(), dto.getKey(), dto.getVal(), dto.getTime(), dto.getScore());
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/removeKey/{id}")
    @ApiOperation(value = "删除所有key")
    public Res<Boolean> removeKey(@ApiParam("主键") @PathVariable("id") String id, int index) {
        Boolean aBoolean = dataSinglesService.removeKey(id, index);
        return Res.ok(aBoolean);
    }

    @PostMapping("/backupDb/{id}")
    @ApiOperation(value = "备份数据")
    public Res<Boolean> backupDb(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RedisBakDto dto) {
        Boolean aBoolean = dataSinglesService.backupDb(id, dto.getIndex(), dto.getPattern());
        return Res.ok(aBoolean);
    }

    @PostMapping("/recoveDb/{id}")
    @ApiOperation(value = "还原数据")
    public Res<Boolean> recoverDb(@ApiParam("主键") @PathVariable("id") String id, @Validated @RequestBody RedisRecoverDto dto) {
        Boolean aBoolean = dataSinglesService.recoverDb(id, dto.getIndex(), dto.getFileId());
        return Res.ok(aBoolean);
    }
}
